from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models

# Create your models here.

from itsdangerous import TimedJSONWebSignatureSerializer as TJS

from meiduo_mall.utils.models import BaseModel


class User(AbstractUser):
    mobile = models.CharField(max_length=11, unique=True, verbose_name='手机号')
    email_active = models.BooleanField(default=False, verbose_name='邮箱验证状态')
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')
    default_address = models.ForeignKey('areas.Address', related_name='users', null=True, blank=True,
                    on_delete=models.SET_NULL, verbose_name='默认地址')

    class Meta:
        # 切记表格名字一定不能有空格, 不然会疯狂报错的
        db_table = 'db_user'
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    def generate_verify_email_url(self):
        """
        生成验证邮箱的url
        """
        tjs = TJS(settings.SECRET_KEY, 300)
        data = {'user_id': self.id, 'email': self.email}
        token = tjs.dumps(data).decode()
        verify_url = 'http://www.meiduo.site:8080/success_verify_email.html?token=' + token
        return verify_url

